/**
 * 文件名：RpcLogAspect.java
 *
 * 创建人：虞云波 - yuyunbo@3856.cc
 *
 * 创建时间：2017年10月6日 下午5:27:55
 *
 * 版权所有：晟邦物流网络科技有限公司
 */
package com.cbwl.eoas.web.aspect;

import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.cbwl.eoas.cms.api.sysconfig.ISysLogService;
import com.cbwl.eoas.cms.model.sysconfig.SysLogDTO;
import com.cbwl.eoas.cms.util.RequestLogUtil;
import com.cbwl.eoas.common.cache.redis.RedisCache;
import com.cbwl.eoas.common.framework.utils.ExceptionUtils;
import com.cbwl.eoas.common.framework.utils.JSONUtil;
import com.cbwl.eoas.common.util.constain.DicDetailConstant;

/**
 * <p>[描述信息：说明类的基本功能]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月6日 下午5:27:55
 */
@Aspect
@Component
public class RequestLogAspect {

	
	
	private static Logger _log = LoggerFactory.getLogger(RequestLogAspect.class);

	@Autowired
	private RedisCache<String,Object> redisCache;
	
	
	@Autowired
	private ISysLogService sysLogService;

//	@Before("execution(* *..controller..*.*(..))")
//	public void doBeforeInServiceLayer(JoinPoint joinPoint) {
//		_log.debug("doBeforeInServiceLayer");
//		
//	}
//
//	@After("execution(* *..controller..*.*(..))")
//	public void doAfterInServiceLayer(JoinPoint joinPoint) {
//		_log.debug("doAfterInServiceLayer");
//	}

	@Around("execution(* com.cbwl.eoas.web.controller..*.*(..))")
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		
		// 开始时间
		long startTime = System.currentTimeMillis();
		// 结束时间
		long endTime = 0L;
		SysLogDTO bean = new SysLogDTO();
		Object result = null;
		long runtime =0;
		
		Object[] obejct = pjp.getArgs();
		if(obejct !=null && obejct.length>0){
			
			for(int i=0;i<obejct.length;i++){
				if(obejct[i] instanceof HttpServletRequest){
					HttpServletRequest request = (HttpServletRequest)obejct[i];
					String tokenId = request.getParameter("tokenId");
					bean = RequestLogUtil.getRequestSysLog(request);
					
					bean.setGmtCreate(new Date());
					if(!StringUtils.isEmpty(tokenId) && !StringUtils.isEmpty(redisCache.getStr(tokenId))){
						Long id = Long.valueOf(redisCache.getStr(tokenId));
						if(id !=null){
							bean.setCreater(id);
						}
					}
				}
			}
		}
//		
		
		
		
		try {
			result = pjp.proceed();
			endTime = System.currentTimeMillis();
			runtime = (endTime-startTime);
			bean.setSysResResult(DicDetailConstant.sysresresult_1);
		} catch (Exception ex) {
			// EOAS TODO: handle exception
			bean.setSysResResult(DicDetailConstant.sysresresult_2);
			bean.setSysExceptionInfo(ExceptionUtils.getTrace(ex).length()>512?ExceptionUtils.getTrace(ex).substring(0, 512):ExceptionUtils.getTrace(ex));
			
		}
		
		bean.setSysResTime(runtime);
		sysLogService.saveObject(bean);
		
		//消耗的时间
//		MDC.put("runtime", String.valueOf(runtime));
//		MDC.put("ip", bean.getSysClientIp());
		
		//参数列表
		String args = Arrays.toString(pjp.getArgs());
		String methodString = pjp.getSignature().getDeclaringTypeName()+"."+pjp.getSignature().getName();
		
		_log.info(JSONUtil.getJSONString(bean));
		
		MDC.clear();
		
//		// 是否是消费端
//		boolean consumerSide = RpcContext.getContext().isConsumerSide();
//		// 获取最后一次提供方或调用方IP
//		String ip = RpcContext.getContext().getRemoteHost();
//		// 服务url
//		String rpcUrl = RpcContext.getContext().getUrl().getParameter("application");
//		_log.info("consumerSide={}, ip={}, url={}", consumerSide, ip, rpcUrl);
		
	
		return result;
	}
}
